
/**		
 * 
 *	uk.co.lmfm.guppy.view.components.update.UpdateView
 *	
 *	@version 1.00 | Jan 12, 2010
 *	@author Justin Windle
 *  
 **/
 
package uk.co.lmfm.guppy.view.components.update 
{
	import uk.co.lmfm.guppy.view.components.events.UpdateViewEvent;

	import flash.events.MouseEvent;

	import com.bit101.components.PushButton;
	import com.bit101.components.ProgressBar;
	import com.bit101.components.Text;

	import uk.co.lmfm.guppy.view.components.core.AbstractView;
	import uk.co.lmfm.guppy.view.components.core.IViewComponent;

	/**
	 * UpdateView
	 */
	public class UpdateView extends AbstractView implements IViewComponent 
	{
		//	----------------------------------------------------------------
		//	CONSTANTS
		//	----------------------------------------------------------------

		private static const MAX_ACTIVITY_ITEMS : int = 15;

		//	----------------------------------------------------------------
		//	PRIVATE MEMBERS
		//	----------------------------------------------------------------

		private var _activity : Text = new Text();		private var _message : Text = new Text();

		private var _activityItems : Array = [];
		private var _lastMessage : String;

		private var _itemProgress : ProgressBar = new ProgressBar();		private var _totalProgress : ProgressBar = new ProgressBar();

		private var _updateButton : PushButton = new PushButton();

		//private var _settingsButton : PushButton = new PushButton();

		//	----------------------------------------------------------------
		//	CONSTRUCTOR
		//	----------------------------------------------------------------

		public function UpdateView()
		{
			_updateButton.label = "Re-Download Library";
			//_settingsButton.label = "Apply Updates";

			_updateButton.addEventListener(MouseEvent.CLICK, onButtonPressed);			//_settingsButton.addEventListener(MouseEvent.CLICK, onButtonPressed);

			addChild(_activity);
			addChild(_message);
			addChild(_itemProgress);			addChild(_totalProgress);
			//addChild(_settingsButton);			addChild(_updateButton);
		}

		//	----------------------------------------------------------------
		//	PUBLIC METHODS
		//	----------------------------------------------------------------

		public function addActivity(text : String) : void
		{
			var lines : Array = text.split("\n");
			
			for (var i : int = 0;i < lines.length;i++) 
			{
				if(lines[i].replace(/\s/g, '').length > 1)
				{
					_activityItems.push(lines[i].replace(/^\s+/, ''));
				}
			}
			
			while(_activityItems.length > MAX_ACTIVITY_ITEMS)
			{
				_activityItems.shift();
			}

			_activity.text = _activityItems.join("\n") + "\n...";
		}
		
		public function setMessage(text : String) : void
		{
			_lastMessage = _message.text = text;
		}

		public function setItemProgress(value : Number) : void
		{
			_itemProgress.value = value;
			_message.text = _lastMessage + "\n" + Number(value * 100).toFixed(1) + "% complete...";
		}

		public function setTotalProgress(value : Number) : void
		{
			_totalProgress.value = value;
		}
		
		public function clear():void
		{
			_activityItems = [];
			_activity.text = '';
			_lastMessage = _message.text = '';
		}

		//	----------------------------------------------------------------
		//	PROTECTED METHODS
		//	----------------------------------------------------------------

		override protected function resize() : void
		{
			super.resize();
			
			_activity.x = 10; 
			_activity.y = 10;
			_activity.width = stage.stageWidth - 20;
			_activity.height = 200;
			
			_message.width = _activity.width;
			_message.height = 40;
			_message.x = _activity.x;
			_message.y = _activity.y + _activity.height + 10;
			
			_itemProgress.x = 10;
			_itemProgress.y = _message.y + _message.height + 10;
			_itemProgress.width = _activity.width;
			
			_totalProgress.x = 10;
			_totalProgress.y = _itemProgress.y + _itemProgress.height + 10;
			_totalProgress.width = _activity.width;
			
			_updateButton.x = stage.stageWidth - _updateButton.width - 10;
			_updateButton.y = _totalProgress.y + _totalProgress.height + 10;
			
			/*_settingsButton.x = _updateButton.x - _updateButton.width - 10;
			_settingsButton.y = _updateButton.y;*/
		}

		//	----------------------------------------------------------------
		//	EVENT HANDLERS
		//	----------------------------------------------------------------

		private function onButtonPressed(event : MouseEvent) : void
		{
			switch(event.currentTarget)
			{
				case _updateButton :
				
					dispatchEvent(new UpdateViewEvent(UpdateViewEvent.FORCE_UPDATE));
				 
					break;
									/*case _settingsButton : 
				
					dispatchEvent(new UpdateViewEvent(UpdateViewEvent.APPLY_SETTINGS));
				
					break;*/
			}
		}
	}
}
